﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Obra.Api;
using System.Globalization;

namespace Obra.SmugMug.Api
{
    public class AlbumContract : 
        AlbumContractBase,
        IAlbum
    {
        [XmlAttribute("Description")]
        public string Description
        {
            get;
            set;
        }

        [XmlAttribute("ImageCount")]
        public int ImageCount
        {
            get;
            set;
        }

        [XmlAttribute("Key")]
        public string Key
        {
            get;
            set;
        }

        //Keywords="sun, kids, boys"
        [XmlAttribute("Keywords")]
        public string Keywords
        {
            get;
            set;
        }

        /// <summary>
        /// SmugMug does not provide the date in XSD format, which makes the 
        /// code a little bit more complicated.  If they did, this would just
        /// work.
        /// </summary>
        [XmlAttribute("LastUpdated")]
        public string LastUpdatedString
        {
            get;
            set;
        }

        /// <summary>
        /// Convert SmugMug's format into .Net DateTime.
        /// </summary>
        public DateTime LastModified
        {
            get { return SmugMugDateTime.Parse(this.LastUpdatedString); }
        }

        [XmlAttribute("Position")]
        public int Position
        {
            get;
            set;
        }

        [XmlAttribute("Title")]
        public string Title
        {
            get;
            set;
        }

        [XmlElement("Category")]
        public CategoryContract Category
        {
            get;
            set;
        }

        [XmlElement("SubCategory")]
        public SubCategoryContract SubCategory
        {
            get;
            set;
        }

        private readonly Dictionary<string, ImageContract> imageContractMap =
            new Dictionary<string, ImageContract>();

        /// <summary>
        /// A map between image name, and the corresponding ImageContract.
        /// </summary>
        [XmlIgnore]
        internal Dictionary<string, ImageContract> ImageContractMap
        {
            get { return imageContractMap; }
        }

        #region IAlbum Members

        /// <summary>
        /// Does this album contain a media with the specified name.
        /// </summary>
        /// <param name="mediaName">
        /// Name of the media to match.
        /// </param>
        /// <returns>
        /// True if the media exists.
        /// </returns>
        public bool Contains(string mediaName)
        {
            return this.ImageContractMap.ContainsKey(mediaName);
        }

        public ICollection<IMedia> Media
        {
            get { return this.imageContractMap.Select(x => (IMedia)x.Value).ToList(); }
        }

        /// <summary>
        /// Get the IMedia for the specified mediaName.
        /// </summary>
        /// <param name="mediaName">
        /// The media name to fetch.
        /// </param>
        /// <returns>
        /// IMedia
        /// </returns>
        /// <exception cref="KeyNotFoundException">
        /// Thrown if no matching media exists.
        /// </exception>
        public IMedia this[string mediaName]
        {
            get { return (IMedia)this.imageContractMap[mediaName]; }
        }

        #endregion

        #region IEntity Members

        /// <summary>
        /// Name of this entity.
        /// </summary>
        [XmlIgnore]
        public string Name
        {
            get { return this.Title; }
        }

        /// <summary>
        /// Unique identifier of this entity.
        /// </summary>
        [XmlIgnore]
        string IEntity.Id
        {
            get { return this.Id.ToString(); }
        }

        private readonly List<string> tags = new List<string>();

        /// <summary>
        /// Tags for this entity.
        /// </summary>
        [XmlIgnore]
        public IList<string> Tags
        {
            get { return tags; }
        }

        #endregion
    }
}